<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $fields=array('id','status','title','logo','url','createtime','android','ios');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('core_account_app',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($_SESSION['isfounder']){
		$tip='《超级管理员可见》：考虑打包APP的专业性，可自行寻求市场上的第三方来打包APP！比如：HBuilder工具打包APP!';
	}else{
		$tip='【温馨提示】：手动打包APP，请联系平台客服！';
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'APP管理'),
			array('op'=>'edit','title'=>'添加APP'),
		),
		'list'=>array('data'=>'list','tip'=>$tip,'list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'logo','title'=>'图标','type'=>'image'),
			array('name'=>'url','title'=>'地址','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time')
		),'action'=>array(
			array('type'=>'post','op'=>'create_android','title'=>'生成安卓版'),
			array('type'=>'post','show'=>'android','op'=>'download_android','title'=>'下载安卓版'),
			array('type'=>'post','op'=>'create_ios','title'=>'生成IOS版'),
			array('type'=>'post','show'=>'ios','op'=>'download_ios','title'=>'下载IOS版'),
		)),
	);
	$return['htmls']=$htmls;
	set_html('header','common/header');
	set_html('title','APP');
	exi($return);
}
if($_RQ['op']=='create_android'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要打包的APP','error');
	}
	exi('为保障APP稳定/质量,目前只支持手动打包APP！请联系管理员手动打包！','error');
	$app=pdo_get('core_account_app',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('title','logo','url'));
	if(!$app){
		exi('指定的APP不存在！','error');
	}
	if(substr($app['url'],0,11)=='./index.php'){
		$app['url']=get_account_url().'custom'.substr($app['url'],1);
	}
	$app['logo']=tomedia($app['logo']);
	$apk=cloud_request('app.create.android',$app);
	$apk_url=cfc('files')->source_save($apk,'data','apk','APP“'.$app['title'].'”安卓版文件');
	pdo_update('core_account_app',array('android'=>$apk_url),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('成功生成安卓版APP');
}
if($_RQ['op']=='create_ios'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要打包的APP','error');
	}
	exi('为保障APP的稳定/质量,目前只支持手动打包APP！请联系管理员手动打包！','error');
	$app=pdo_get('core_account_app',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('title','logo','url'));
	if(!$app){
		exi('指定的APP不存在！','error');
	}
	$app['logo']=tomedia($app['logo']);
	$apk=cloud_request('app.create.ios',$app);
	$apk_url=cfc('files')->source_save($apk,'data','ipa','APP“'.$app['title'].'IOS版文件');
	pdo_update('core_account_app',array('ios'=>$apk_url),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('成功生成IOS版APP');
}
if($_RQ['op']=='download_android'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要下载的APP','error');
	}
	$url=pdo_getcolumn('core_account_app',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'android');
	if(!$url){
		exi('指定的APP不存在或者尚未生成iOS版安装文件！','error');
	}
	$url=tomedia($url);
	exi('','','',$url);
}
if($_RQ['op']=='download_ios'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要下载的APP','error');
	}
	$url=pdo_get('core_account_app',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'ios');
	if(!$url){
		exi('指定的APP不存在或者尚未生成iOS版安装文件！','error');
	}
	$url=tomedia($url);
	exi('','','',$url);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_app',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		//检查是否超出个数限制
		if($_SESSION['account']['groupid']){
			$limit=pdo_getcolumn('core_account_group',array('id'=>$_SESSION['account']['groupid']),'app_num');
			$num=pdo_count('core_account_app',array('uniacid'=>$_SESSION['uniacid']));
			if($num>=$limit){
				exi('您所添加的app已达到最大值，如需创建更多，请联系管理员提升权限或者升级套餐！','error','goback');
			}
		}
		$item=array();
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'APP设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'APP管理'),
			array('op'=>'edit','title'=>'添加APP')
		),
		'edit'=>array(
			array('type'=>'string','title'=>'名称','name'=>'title', 'help'=>'' ),
			array('type'=>'image','title'=>'图标','name'=>'logo', 'help'=>'' ),
			array('type'=>'link','title'=>'地址','name'=>'url', 'help'=>'' ),
			)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('title','logo','url'),$postdata);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_account_app',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('core_account_app',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_app',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_app',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
?>